import { _decorator, Component, Node, AudioClip, AudioSource } from 'cc';
const { ccclass, property } = _decorator;

interface EffectList {
    [name: string]: AudioClip
}
@ccclass('AudioMain')
export class AudioMain extends Component {
    // 背景音乐
    @property(AudioClip)
    public backgroundMusic = null

    // 子弹音效列表
    @property([AudioClip])
    public bulletEffect = []
    // 敌方阵亡音效
    @property(AudioClip)
    public enemyDieEffect = null
    // 我方阵亡音效
    @property(AudioClip)
    public playerDieEffect = null
    // 按钮音效
    @property(AudioClip)
    public buttonEffect = null
    // 用于存放所有音效
    private _effectList: EffectList = {}
    // 用于播放音效
    private _player: AudioSource = new AudioSource('player')

    // 播放子弹音效
    public bulletEffectPlay(i: number) {
        const index = i % this.bulletEffect.length
        this._player.playOneShot(this.bulletEffect[index])
        // this.bulletEffect[index].playOneShot()
    }
    // 根据名字播放音效
    public play(name: string) {
        this._effectList[name] && this._player.playOneShot(this._effectList[name])
    }

    start() {
        // 播放背景音乐
        this._player.clip = this.backgroundMusic
        this._player.loop = true
        this._player.volume = 0.8
        this._player.play()
        // 存放所有音效
        this._effectList = {
            enemyDie: this.enemyDieEffect,
            playerDie: this.playerDieEffect,
            button: this.buttonEffect
        }
    }

    update(deltaTime: number) {

    }
}

